
import { promises as fs } from 'fs';
import path from 'path';

const ROOT = process.cwd();
const SRC = path.join(ROOT, "src");
const OUT = path.join(ROOT, "assets", "classlist.json");
const CLASS_RE = /class\s*=\s*([\w:]+)?=?\s*([\"'])(.*?)\2/gs;

async function walk(dir) {
  const acc = [];
  for (const e of await fs.readdir(dir, { withFileTypes: true })) {
    const p = path.join(dir, e.name);
    if (e.isDirectory()) acc.push(...(await walk(p)));
    else if (e.isFile() && p.endsWith(".rs")) acc.push(p);
  }
  return acc;
}
const split = s => s.split(/\s+/).map(x=>x.trim()).filter(Boolean);

const main = async () => {
  const files = await walk(SRC);
  const set = new Set();
  for (const f of files) {
    const txt = await fs.readFile(f, "utf8");
    for (const m of txt.matchAll(CLASS_RE)) split(m[3]).forEach(c=>set.add(c));
  }
  const arr = Array.from(set).sort();
  await fs.mkdir(path.dirname(OUT), { recursive: true });
  await fs.writeFile(OUT, JSON.stringify(arr, null, 2), "utf8");
  console.log(`Safelist wrote ${arr.length} classes -> ${OUT}`);
};
main().catch(e => { console.error(e); process.exit(1); });
